﻿Option Strict On

Imports System.Data.SQLite
Imports Sunts.DataLayer.SQLite
Imports System.Management

Public Class HyperVHost

    Public Enum ConnectableState
        Connectable
        AccessIsDenied
        NamespaceNotFound
        Unknown
    End Enum

    Public Property HostID As Integer
    Public Property Hostname As String
    Public Property Username As String
    Public Property Password As String
    Public Property ComputerName As String
    Public ReadOnly Property Exception As Exception
        Get
            Return _exception
        End Get
    End Property
    Private _exception As Exception

    Public Function Insert(ByVal ConnectionString As String) As Int32
        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "INSERT INTO HostServers (Hostname, Username, Password, ComputerName) VALUES (@Hostname, @Username, @Password, @ComputerName)"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@Hostname", Me.Hostname))
                sdl.ParameterCollection.Add(New SQLiteParameter("@Username", Me.Username))
                sdl.ParameterCollection.Add(New SQLiteParameter("@Password", Me.Password))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ComputerName", Me.ComputerName))

                Insert = sdl.ExecuteNonQuery()

            End Using
        Catch ex As Exception
            Throw New Exception("HyperVHost: Insert()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try
    End Function

    Public Shared Function SelectAll(ByVal ConnectionString As String) As HyperVHostCollection
        Dim HyperVHostCollection As HyperVHostCollection
        Dim HyperVHost As HyperVHost
        Dim Reader As SQLiteDataReader

        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT HostID, HostName, Username, Password, ComputerName FROM HostServers"

                HyperVHostCollection = New HyperVHostCollection

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    HyperVHost = New HyperVHost
                    HyperVHost.HostID = CInt(Reader("HostID"))
                    HyperVHost.Hostname = CStr(Reader("HostName"))
                    HyperVHost.Username = CStr(Reader("Username"))
                    HyperVHost.Password = CStr(Reader("Password"))
                    HyperVHost.ComputerName = CStr(IIf(IsDBNull(Reader("ComputerName")), String.Empty, Reader("ComputerName")))
                    HyperVHostCollection.Add(HyperVHost)
                End While
                Reader.Close()
            End Using

            SelectAll = HyperVHostCollection

        Catch ex As Exception
            Throw New Exception("HyperVHost: SelectAll()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            HyperVHost = Nothing
            HyperVHostCollection = Nothing
            Reader = Nothing
        End Try
    End Function

    Public Shared Function SelectAllRemote(ByVal ConnectionString As String) As HyperVHostCollection
        Dim HyperVHostCollection As HyperVHostCollection
        Dim HyperVHost As HyperVHost
        Dim Reader As SQLiteDataReader

        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT HostID, HostName, Username, Password, ComputerName FROM HostServers WHERE HostName <> 'localhost'"

                HyperVHostCollection = New HyperVHostCollection

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    HyperVHost = New HyperVHost
                    HyperVHost.HostID = CInt(Reader("HostID"))
                    HyperVHost.Hostname = CStr(Reader("HostName"))
                    HyperVHost.Username = CStr(Reader("Username"))
                    HyperVHost.Password = CStr(Reader("Password"))
                    HyperVHost.ComputerName = CStr(IIf(IsDBNull(Reader("ComputerName")), String.Empty, Reader("ComputerName")))
                    HyperVHostCollection.Add(HyperVHost)
                End While
                Reader.Close()
            End Using

            SelectAllRemote = HyperVHostCollection

        Catch ex As Exception
            Throw New Exception("HyperVHost: SelectAllRemote()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            HyperVHost = Nothing
            HyperVHostCollection = Nothing
            Reader = Nothing
        End Try
    End Function

    Public Shared Function SelectByID(ByVal HyperVHostID As Int32, ByVal ConnectionString As String) As HyperVHost
        Dim HyperVHost As HyperVHost
        Dim Reader As SQLiteDataReader

        Try
            HyperVHost = New HyperVHost
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT * FROM HostServers WHERE HostID = @HostID"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@HostID", HyperVHostID))

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    HyperVHost = New HyperVHost
                    HyperVHost.HostID = CInt(Reader("HostID"))
                    HyperVHost.Hostname = CStr(Reader("Hostname"))
                    HyperVHost.Username = CStr(Reader("Username"))
                    HyperVHost.Password = CStr(Reader("Password"))
                    HyperVHost.ComputerName = CStr(IIf(IsDBNull(Reader("ComputerName")), String.Empty, Reader("ComputerName")))
                End While
                Reader.Close()
            End Using

            SelectByID = HyperVHost

        Catch ex As Exception
            Throw New Exception("HyperVHost: SelectByID()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            HyperVHost = Nothing
            Reader = Nothing
        End Try
    End Function

    Public Function GetConnectionState() As ConnectableState
        Dim ConnectionOptions As ConnectionOptions
        Dim ManagementScope As ManagementScope

        Try
            If Me.Hostname = "localhost" Then
                ManagementScope = New ManagementScope("root\virtualization")
            Else
                ConnectionOptions = New ConnectionOptions
                ConnectionOptions.Username = Me.Username
                ConnectionOptions.Password = Me.Password
                ManagementScope = New ManagementScope("\\" & Me.Hostname & "\root\virtualization", ConnectionOptions)
            End If

            Using ManagementObjectSearcher As New ManagementObjectSearcher(ManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem"))
                Using ManagementObjectCollection As ManagementObjectCollection = ManagementObjectSearcher.Get()
                End Using
            End Using

            Return ConnectableState.Connectable

        Catch ex As ManagementException
            _exception = ex
            If ex.ErrorCode = ManagementStatus.InvalidNamespace Then
                Return ConnectableState.NamespaceNotFound
            Else
                Return ConnectableState.Unknown
            End If
        Catch ex As UnauthorizedAccessException
            _exception = ex
            Return ConnectableState.AccessIsDenied
        Catch ex As Exception
            _exception = ex
            Return ConnectableState.Unknown
        Finally
            ConnectionOptions = Nothing
            ManagementScope = Nothing
        End Try
    End Function

    
End Class

Public Class HyperVHostCollection
    Inherits System.Collections.ObjectModel.Collection(Of HyperVHost)

    Public Function ContainsHyperVHost(ByVal HyperVHostName As String) As Boolean
        Dim iEnum = Me.GetEnumerator

        Try
            While iEnum.MoveNext()
                Dim HyperVHost As HyperVHost = iEnum.Current
                If HyperVHost.Hostname = HyperVHostName Then
                    Return True
                End If
            End While

            Return False

        Catch ex As Exception
            Throw New Exception("HyperVHostCollection: ContainsHyperVHost()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            iEnum = Nothing
        End Try



    End Function

    Public Function GetHyperVHost(ByVal HyperVHostName As String) As HyperVHost
        Dim iEnum = Me.GetEnumerator
        Dim HyperVHost As HyperVHost

        Try
            While iEnum.MoveNext()
                HyperVHost = iEnum.Current
                If HyperVHost.Hostname = HyperVHostName Then
                    Return HyperVHost
                End If
            End While

            Return Nothing

        Catch ex As Exception
            Throw New Exception("HyperVHostCollection: GetHyperVHost()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            iEnum = Nothing
            HyperVHost = Nothing
        End Try


    End Function

End Class
